import Vue from "vue";
import confirm from "./Confirm";

const Confirm = Vue.extend(confirm);

const $myConfirm = function(message, title, options) {
  return new Promise((resolve, reject) => {
    if (typeof title === "object") {
      options = title;
      title = "";
    } else if (title === undefined) {
      title = "";
    }
    let data = {};
    data.message = message;
    data.title = title;
    data.resolve = resolve;
    data.reject = reject;
    data = Object.assign(options, data);

    let instance = new Confirm({ data });
    instance.$mount();
    document.body.appendChild(instance.$el);
    Vue.nextTick(() => {
      instance.isVisible = true;
    });
  });
};

export default {
  install(Vue) {
    Vue.prototype.$myConfirm = $myConfirm;
  }
};
